PHP - Strings
Strings are considered as a sequence of characters. You can use the strings within single or double quotes, without quotes you will not be able to perform string operations. Even if you want an integer to represent as string you can enclose that integer within quotes, it will be typecast as a string. Both single and double quotes have a different way of processing a string.
1. Single quotes
The string with a single quote will not process any special character present.
1.1 Printing string with a single quote without any special character.
<php $str=’welcome here’; Echo $str; ?>
Output welcome here
1.2 Printing string with a single quote with a special character.
<?php $str=’welcome here’; Echo ‘hello $str’; ?>
Output hello $str In the second code, the $str will not retrieve the defined value as it is enclosed within a single quote and it prints the string ‘hello $str’ as it is. $ value will get ignored by PHP when considering the single quote.
2. Double quotes
If your purpose will not get solved with a single quote string then you can use double-quoted string. It will help the special characters to get processed. If we consider the above example with double quotes then the special character $ will also be considered and get processed to proved the desired output.
<?php $str=’welcome here’; Echo “hello $str”; ?>
Output hello welcome here Special characters used that can be processed within the double quotes-
- “\n” represents a new line
- “\t” represents a tab space
- “\$” represents a dollar sign
- “\r” represents a carriage return
- “\\” represents a backslash
- “\”” represents a double quote
- “\'” represents a single quote
- $ represents the value assigned to it and gets replaced by the value of that variable.
Built-in String functions
Now we will learn about the string functions and how we can implement them showing the special characteristics of the string. There is no fixed limit for strings as we have for integers, float, and other data types. PHP supports various string functions so that you can perform actions on strings or you can manipulate strings with the help of these functions.
1. Strlen() function
It provides you with the length of the string. This string function takes a single parameter that is the string and returns the length whose counting starts with one by default. This function will also count the space as a blank character.
Example-
<?php $str=”hello man”; Echo strlen($str); ?>
Output 9
2. Strrev() function
It provides you with the reverse of the provided string. This string function will take only one parameter and return the reverse of that string.
Example-
<?php Echo strrev(“hello”); ?>
Output olleh
3. Str_replace() function
This function is used to replace the part of the string with another provided string. This function takes three parameters where the second parameter string will get replaced by the first parameter string. The second parameter string will be present in the third parameter which is the actual string whose part we want to replace. Example
<?php Echo str_replace (“hello”, “bye”, “hello Jacob”); ?>
Output bye Jacob
4. strpos() function
This function returns the starting position of one string that is present in the other given string. This function takes two string parameters if the string is not present then it will return false. If the matched string is the starting character of the other string then the returned position is zero as explained in the below example. Example-
<?php echo strpos("Hello Jacob", "Jacob"), "\n"; echo strpos("Hello Jacob", "H"), "\n"; var_dump(strpos("Hello", "bye")); ?>
Output 6 0 Bool (false)
trim() function will help you to remove any string or whitespaces from both sides of the given string.
Example-
<?php Echo trim("hello Jacob", "he"); echo "\n"; echo trim("hello jacob", "ja"); echo "\n"; echo trim (" hello jacob ","h"); ?>
Output
hello Jacob hello Jacob hello jacob
In the above example, the last trim function is removing a single space from both sides of the string, still, we are left with two more spaces that you can see in the result.
People are also reading: